package com.yuju.hotel.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.hotel.dao.IBuildingDao;
import com.yuju.hotel.dao.entity.Building;
import com.yuju.hotel.model.BuildingModel;
import com.yuju.hotel.model.FloorModel;
import com.yuju.hotel.service.IBuildingService;
import com.yuju.hotel.service.IFloorService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：BuildingServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 上午10:34:24  <br />
 * @version : 1.1 <br />
 */
@Service
public class BuildingServiceImpl extends AbstractBaseService<BuildingModel, Building, String> implements IBuildingService{

	@Autowired
	private IBuildingDao buildingDao;
	@Autowired
	private IFloorService floorService;
	
	
	@Override
	public IBaseDao<BuildingModel, Building, String> getDao() {
		// TODO Auto-generated method stub
		return buildingDao;
	}

	/**
	 * 
	 * 功能: <br/> 楼栋添加操作
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月27日 上午10:01:31<br/>
	 * 
	 * @param buildingModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int save(BuildingModel buildingModel){
		buildingModel.setId(Utils.getUUID());
		buildingModel.setGmtCreate(new Date());
		buildingModel.setGmtModified(new Date());
		return this.getDao().saveSelective(buildingModel);
	}
	/**
	 * 
	 * 功能: <br/>楼栋更新操作
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月27日 上午10:01:57<br/>
	 * 
	 * @param buildingModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#updateSelective(java.lang.Object)
	 */
	@Override
	public int updateSelective(BuildingModel buildingModel){
		buildingModel.setGmtModified(new Date());
		return this.getDao().updateSelective(buildingModel);
	}
	/**
	 * 
	 * 功能:重写删除逻辑，楼栋下有楼层不可删除 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月20日 上午10:11:14<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#del(java.lang.Object)
	 */
	@Override
	public int del(String id){
		//查询楼层下的房间
		FloorModel floorModel = new FloorModel();
		floorModel.setBuildingId(id);
		List<FloorModel> list = floorService.list(floorModel);
		//判断是否有房间数据
		if(list.size() != 0){
			return 0;
		}
		return this.getDao().del(id);
		
	}
	/**
	 * 
	 * 功能:重名验证 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月14日 上午11:41:02<br/>
	 * 
	 * @param buildingModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.IBuildingService#selectByName(com.yuju.hotel.model.BuildingModel)
	 */
	@Override
	public boolean selectByName(BuildingModel buildingModel) {
		boolean result = false;
		//装载数据查询数据库
		BuildingModel model = new BuildingModel();
		model.setName(buildingModel.getName());
		model.setHotelId(buildingModel.getHotelId());
		//修改时验证重名
		if (Utils.isNotNull(buildingModel.getId())) {
			BuildingModel buildingModel1 = convert(this.getDao().getObj(buildingModel.getId()));
			if (buildingModel.getName().equals(buildingModel1.getName())) {
			        result = true;
			    } else {
			   	 	Map<Object, Object> beanMap = getBeanMap(model);
			        List<BuildingModel> list = this.getDao().list(beanMap);
			        if (list.size() == 0) {
			            result = true;
			        }
			    }
		//新增验证重名
		}else {
			Map<Object, Object> beanMap = getBeanMap(model);
			    List<BuildingModel> list = this.getDao().list(beanMap);
			    if (list.size() == 0) {
			        result = true;
			    }
			}
		return result;
	}
	
	
}
